import datetime,cgi
import sys
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
from os import environ
from models import *

class QAPage(webapp.RequestHandler):
	global questionid
	
	def post(self):
		global questionid
		answer=self.request.get("ans")
		question=db.GqlQuery("SELECT * FROM questionanswer WHERE questionid='%s'" %questionid)
		for qn in question:
			qn.answer=answer
			qn.put()
		self.redirect("/answerquestionmain")

	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		global questionid
		questionid=''
		url = self.request.url
		for i in range(0,len(url),1):
			if(url[i]=='?'):
				i=i+1
				while(url[i]!='='):
					questionid=questionid+url[i]
					i=i+1
				break
		question=db.GqlQuery("SELECT * FROM questionanswer WHERE questionid='%s' AND answer=NULL" %questionid)
		for qn in question:
			path = os.path.join(os.path.dirname(__file__),'qnans.html')#use template
			self.response.out.write(template.render(path,{"question":qn.question,"questionid":qn.questionid}))#using template

class QAPageMain(webapp.RequestHandler):
	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		unansweredqns=db.GqlQuery("SELECT * FROM questionanswer WHERE answer=NULL")
		ques=[]
		for question in unansweredqns:
			temp=[]
			temp.append(question.question)
			temp.append(question.questionid)
			ques.append(temp)
		path = os.path.join(os.path.dirname(__file__),'qnansmain.html')#use template
		self.response.out.write(template.render(path,{"question":ques}))#using template

application = webapp.WSGIApplication([('/answerquestion', QAPage),('/answerquestionmain', QAPageMain)], debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
